<?
if($check_me_authed == "OK"){

	$today_date				  = date("d-m-Y");
	$txType             = _PEMBELIAN;
  $refIdTag           = "43";
	$srcDbSequence      = "MST_SEQUENCE";
  $srcDbHdr           = "PCH_HDR";
  $srcDbHdrLog        = "LOG_PCH_HDR";
  $srcDbDtl           = "PCH_DTL";
  $srcDbSubDtl        = "PCH_SUB_DTL";
  $srcDbMstModel      = "MST_MODEL";
  $srcDbInputHistory  = "MODEL_INPUT_HISTORY";
  $srcDbSupplier      = "SUPPLIER";

	if($_POST["selbox"] != ''){

		$UID     = unserialize($_SESSION["serUID"]);
    $userId  = $UID->getUID();

    $COracle = new oracleDb;
    $COracle->init($_SESSION["ORCL"],
                   $_SESSION["OPASS"],
                   $_SESSION["SID"],
                   _NO_COMMENT);

    if($COracle->connect()){

			if(sizeof($_POST["selbox"]) == 0){

				$Misc->usrDie("Error: Nothing to approve");
			}else{

				foreach($_POST["selbox"] as $apprThisJobId){

					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE
      		*
      		*/
      		$CFetchers->fetchLstDataHeader($COracle,
                                         $apprThisJobId,
                                         $srcDbHdr,
                                         $nHeader,
                                         $lstHeader);

				  if($lstHeader["STATUS"][0] == _NOT_ACTIVE){

			      /**
			      * Build up the REFERENCE number
			      *
			      */
			      $thisPeriode    = &$_SESSION["sysPeriode"];

			      $seqEditStatus  = false;

            $thisSequence   = "PCH_REF_SEQ";

          	while($seqEditStatus == false){

            	$CFetchers->fetchCurSequence($COracle,
                          	               $thisSequence,
                          	               $thisPeriode,
                          	               $srcDbSequence,
                        	                 $srcJidIndex);

            	$CHandlers->initCurSequence($COracle,
                          	              $thisSequence,
                          	              $srcJidIndex,
                          	              $thisPeriode,
                          	              $srcDbSequence,
                          	              $seqEditStatus);
            }

            /**
            * Extract the system period
            *
            */
            $tmpDate = explode("-", $thisPeriode);
            $wrkDate = $tmpDate[0]. /* This period month */
                       "-".
                       $tmpDate[1]; /* This period year */

            $refId  = $Misc->addDigit((string) $srcJidIndex).
                      $srcJidIndex.
                      "/" .
                      $refIdTag.
                      "/" .
                      $wrkDate;

            $CHandlers->procApproval($COracle,
                                     $apprThisJobId,
                                     $refId,
                                     $userId,
                                     $srcDbHdr);

            $jobId     = &$apprThisJobId;
            $isApprove = true;

            $periode = " TO_DATE('".$_SESSION["sysPeriode"]."','mm-YYYY') ";

            $txDate  = " TO_DATE('".date("d-m-Y",
                                        strtotime($lstHeader["TR_DATE"][0]))."',
                                 'dd-mm-YYYY') ";

            $dueDate = " TO_DATE('".date("d-m-Y",
                                        strtotime($lstHeader["DUE_DATE"][0]))."',
                                 'dd-mm-YYYY') ";

            require("controllers/procStock.php");
            require("controllers/procSnWhs.php");

            require("controllers/procAP.php");
            require("controllers/procDp.php");

            /**
            * Insert Log
            *
            */
            $CHandlers->insToDbLog($COracle,
                                   $today_date,
                                   $apprThisJobId,
                                   $userId,
                                   $_SERVER["REMOTE_ADDR"],
                                   _APPROVE_MODE,
                                   $srcDbHdrLog);

				  }else{

				    $Misc->usrDie("Error: Tx is ACTIVE/APPROVED/CANCELED");
				  }

				}

				$COracle->commit();

				if($COracle->getStatus() == true){

      	  $Misc->usrMsg("Approval Complete");
      	}else{

      	  $Misc->usrMsg("Approval Process Interupted");
      	}

      	$COracle->dc();

			}
		}else{

			$Misc->usrDie("Error: oracle connection failed");
		}
	}else{

		$Misc->usrDie("Error: Unknown selection or bad ID retrieved");
		//jumpTo(2,"../".$_SESSION["source_caller"]);
	}
}else{

	$Misc->usrDie("NOT AUTHED");
}
?>